package logic

import (
	"context"
	"errors"
	"house_srv/models/mysql"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListLogic) HouseList(in *house.HouseListRequest) (*house.HouseListResponse, error) {
	// todo: add your logic here and delete this line
	h := mysql.House{}
	list, err := h.HouseList(in.Keyword, in.Page, in.PageSize, in.StarPrice, in.EndPrice)
	if err != nil {
		logx.Info("房源列表信息失败", err)
		return nil, errors.New("房源列表信息失败")
	}
	var slice []*house.HouseList

	for _, v := range list {
		houseList := house.HouseList{
			UserId:      v.UserId,
			HouseId:     int64(v.ID),
			HouseName:   v.HouseName,
			HousePrice:  v.HousePrice,
			Floor:       v.Floor,
			Acreage:     v.Acreage,
			Address:     v.Address,
			HouseType:   v.HouseType,
			HouseStatus: v.HouseStatus,
		}
		slice = append(slice, &houseList)
	}
	return &house.HouseListResponse{
		List: slice,
	}, nil
}
